// pages/yuyue/yuyue.js
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Name: '',
    BusinessLicense: '',
    Project: '',
    ContactsName: '',
    Phone: '',
    Address: '',
    Introduce: '',
    Img: '',
    Imgs: null,
    backTopValue: false,// 是否返回顶部
    openflag: true,//判断使用哪个模板
  },

  //点击打电话开始
  calling: function () {
    wx.makePhoneCall({
      phoneNumber: '020-36083228',
      success: function () {
        //console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  //点击打电话结束

  close: function () {
    this.setData({
      openflag: false
    });
  },
  open: function () {
    this.setData({
      openflag: true
    });
  },

  //点击回到顶部开始
  onPageScroll: function (e) {
    //console.log(e)
    var that = this;
    var scrollTop = e.scrollTop;
    var backTopValue = scrollTop > 300 ? true : false;
    that.setData({
      backTopValue: backTopValue
    });
  },
  // 滚动到顶部
  backTop: function () {
    // 控制滚动
    var that = this;
    //console.log(that);
    if (that.data.backTopValue == true) {
      wx.pageScrollTo({
        scrollTop: 0
      })
      that.setData({
        backTopValue: false
      });
    }
  },
  //点击回到顶部结束

  //跳转首页
  skipIndex: function () {
    wx.reLaunch({     //跳转至指定页面并关闭其他打开的所有页面（这个最好用在返回至首页的的时候）
      url: "/pages/customIndex/index"
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 输入商户名称
   */
  bindNameInput: function (event) {
    var that = this;
    that.setData({
      Name: event.detail.value
    })
  },

  /**
    * 输入营业执照号
    */
  bindBusinessLicenseInput: function (event) {
    var that = this;
    that.setData({
      BusinessLicense: event.detail.value
    })
  },

  /**
    * 输入主营项目
    */
  bindProjectInput: function (event) {
    var that = this;
    that.setData({
      Project: event.detail.value
    })
  },

  /**
    * 输入联系人姓名
    */
  bindContactsNameInput: function (event) {
    var that = this;
    that.setData({
      ContactsName: event.detail.value
    })
  },

  /**
   * 输入联系电话
   */
  bindPhoneInput: function (event) {
    var that = this;
    that.setData({
      Phone: event.detail.value
    })
  },

  /**
   * 输入详细地址
   */
  bindAddressInput: function (event) {
    var that = this;
    that.setData({
      Address: event.detail.value
    })
  },

  /**
   * 输入简单介绍
   */
  bindIntroduceInput: function (event) {
    var that = this;
    that.setData({
      Introduce: event.detail.value
    })
  },

  //上传图片
  chooseImageTap: function (event) {
    let that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },

  chooseWxImage: function (type) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          Imgs: res.tempFilePaths,
        })
        var strimg = '';
        for (var index in res.tempFilePaths) {//逐个上传http://img4.winmobi.cn/Baidu/Upload'
          that.upload_file('https://img4.winmobi.cn/Baidu/Upload', res.tempFilePaths[index], 'photo',
            function (res) {
              //调用上传文件成功
              //console.log(res);
              var resData = JSON.parse(res)
              strimg += resData.dataUrl + ',';
              //strimg = resData.dataUrl;
              console.log(strimg);
              that.setData({
                Img: strimg
              })

            },
            function (res) {
              //调用上传文件失败
              wx.showModal({
                title: '警告',
                content: '上传图片失败',
                showCancel: false
              })
            })
        }
      }
    })
  },

  //上传文件
  upload_file: function (url, filePath, name, success, fail) {
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: name,
      formData: {
        'customerid': appData.custID,
        'userid': wx.getStorageSync('UserInfo').UserID,
        'imageType': 0,
        'imageSize': 0,
        'isWeixin': 0
      },// HTTP 请求中其他额外的参数
      header: {
        'content-type': 'multipart/form-data'
      }, // 设置请求的 header
      success: function (res) {
        if (res.statusCode == 200 && !res.data.result_code) {
          typeof success == "function" && success(res.data);
        } else {
          typeof fail == "function" && fail(res);
        }
      },
      fail: function (res) {
        console.log(res);
        typeof fail == "function" && fail(res);
      }
    })
  },

  /**
* 提交保存入驻申请
*/
  bindSaveTap: function (event) {
    var that = this;
    var pattern = /^1[34578]\d{9}$/;
    if (that.data.Name == "" || that.data.Name == null) {
      wx.showModal({
        title: '警告',
        content: '请输入商户名称',
        showCancel: false
      })
      return;
    }
    if (that.data.Phone == "" || that.data.Phone == null) {
      wx.showModal({
        title: '警告',
        content: '请输入手机号码',
        showCancel: false
      })
      return;
    }
    if (!pattern.test(that.data.Phone)) {
      wx.showModal({
        title: '警告',
        content: '号码不对,请重新输入',
        showCancel: false
      })
      return;
    }
    if (that.data.BusinessLicense == "" || that.data.BusinessLicense == null) {
      wx.showModal({
        title: '警告',
        content: '请输入营业执照',
        showCancel: false
      })
      return;
    }
    if (that.data.Project == "" || that.data.Project == null) {
      wx.showModal({
        title: '警告',
        content: '请输入主营项目',
        showCancel: false
      })
      return;
    }
    if (that.data.ContactsName == "" || that.data.ContactsName == null) {
      wx.showModal({
        title: '警告',
        content: '请输入联系人姓名',
        showCancel: false
      })
      return;
    }
    if (that.data.Address == "" || that.data.Address == null) {
      wx.showModal({
        title: '警告',
        content: '请输入地址',
        showCancel: false
      })
      return;
    }
    var param = {
      CustID: appData.custID,
      Name: that.data.Name,
      Phone: that.data.Phone,
      BusinessLicense: that.data.BusinessLicense,
      Project: that.data.Project,
      ContactsName: that.data.ContactsName,
      Address: that.data.Address,
      Introduce: that.data.Introduce,
      Img: that.data.Img,
    };
    //加载数据绑定
    wx.showLoading({ title: '保存中...', mask: true });
    //console.log(param);//appData.apiurl + 'mobile/api/Home_Appointment/Add'
    that.wxSubRequest('POST', appData.apiurl + 'mobile/api/Home_Storecooperation/Add', param,
      function (data) {
        if (data.code == 1200) {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '申请成功',
            showCancel: false
          })
          //that.setData({
          // CneeId: data.content.CneeID
          //})
          // var url = '/pages/deliveryAddr/deliveryAddr';
          // if (that.data.From == 'order' && that.data.IsDef == 1) {
          //   url = '/pages/submitOrder/submitOrder?CneeID=' + that.data.CneeId;
          // }
          //跳转页面
          wx.switchTab({
             url: "/pages/my/my"
          })
        }
      },
      function (obj) {
        wx.hideLoading()
        console.log(obj)
      })
  },

  /**
   * 微信提交请求获取数据或保存数据
   * 
   */
  wxSubRequest: function (method, url, param, callBack, CallBackError) {
    wx.request({
      url: url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        if (res.statusCode == 200 && res.data.code == 1200) {
          var data = res.data;
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function (res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.msg)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})